﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CXDB;
using Coolite.Ext;
using Coolite.Ext.Web;
using SubSonic;
using ListItem=Coolite.Ext.Web.ListItem;


public partial class HR_UserListReply : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Cookies.getCookies("cLevelID")!="1")
        {
            Window1.Show();
        }
        if(!IsPostBack)
        {
            Store1Bind();
        }
    }
    protected void RowSelect(object sender, AjaxEventArgs e)
    {
         
        string id = e.ExtraParams["ida"];
        IDLabel.Text = id;
        rID.Value = id;
        Query q = new Query(VwStaffHrStep1.Schema);
        q.AddWhere("id", id);
        IDataReader dr = q.ExecuteReader();
        if(dr.Read())
        {
            Tfguid.Text = dr["guid"].ToString();

            string[] str = CXFileUpload.show(new Guid(dr["guid"].ToString())).Split('$');
            tffileshow.NavigateUrl = str[0];
            tffileshow.Text = str[1];


            TfUserId.Text =dr["userid"].ToString();
            TfUserName.Text = dr["ED_username"].ToString();
            TfDeptName.Text = dr["ED_DeptName"].ToString();
            TfTalkTime.Value = dr["talktime"].ToString();
            TfDeptID.Text = dr["DeptID"].ToString();
            TfTalkContent.Text = dr["TalkContent"].ToString();
            TfPunish_party_time.Value = dr["punish_party_time"].ToString();
            TfPunish_party_reason.Text = dr["punish_party_reason"].ToString();
            TfPunish_party_type.Text = dr["punish_party_type"].ToString();
            TfPunish_penal_reason.Text = dr["punish_penal_reason"].ToString();
            TfPunish_penal_time.Value = dr["punish_penal_time"].ToString();
            TfPunish_penal_type.Text = dr["punish_penal_type"].ToString();
            TfParty_Problem.Text = dr["party_problem"].ToString();
            TfOther_Problem.Text = dr["other_problem"].ToString();
            TfEntering_username.Text = dr["Enter_username"].ToString();
            TfaDept_master_remark.Text = dr["aDept_master_remark"].ToString();
            TfDept_Master_Remark.Text = dr["dept_master_remark"].ToString();
            TfBoss_Remark.Text = dr["boss_remark"].ToString();

            TfaDept_Master_Status.Reset(); 
            TfaDept_Master_Status.SetInitValue(dr["aDept_master_status"].ToString());
            
            TfBoss_ID.Reset();
            Store3Bind();
            TfBoss_ID.SetInitValue(dr["Boss_userid"].ToString());

            TfDept_Master_ID.Reset();
            TfDept_Master_ID.SetInitValue(dr["dept_master_ID"].ToString());

            TfBoss_Status.Reset();
            TfBoss_Status.SetInitValue(dr["Boss_Status"].ToString());
            
            TfDept_master_status_Comb.Reset();
            TfDept_master_status_Comb.SetInitValue(dr["Dept_master_status"].ToString());

            if (dr["Boss_Status"].ToString() == dr["Dept_master_status"].ToString() && dr["Dept_master_status"].ToString() == dr["aDept_master_status"].ToString() && dr["aDept_master_status"].ToString() == "1")
            {
                Button1.Disabled = true;
            }
            else
            {
                Button1.Disabled = false;
            }
            
           
            //人事领导审批
            if((Cookies.getCookies("cDeptID")=="3")&&(Cookies.getCookies("cLevelID")=="1"))
            {
                TfaDept_Master_Status.Disabled = false;
                TfaDept_master_remark.Disabled = false;
                TfDept_Master_ID.Disabled = false;
                TfBoss_ID.Disabled = false;
            }

            //公司领导审批框
            if ((Cookies.getCookies("cDeptID") == "1")&&Cookies.getCookies("cLevelID")=="1")
            {
                    TfBoss_Remark.Disabled = false;
                    TfBoss_Status.Disabled = false;
               
            }
            //部门领导审批框
            if ((Cookies.getCookies("cDeptID") != getDeptId(int.Parse(dr["deptid"].ToString())).ToString()))
            {
                TfDept_Master_Remark.Disabled = true;
                TfDept_master_status_Comb.Disabled = true;
            }
             
        }
    }

    protected void SubClick(object s, AjaxEventArgs e)
    {
        if (rID.Value.ToString() == "")
        {
            Err.Warning("请从左侧列表框选择一个记录，再进行此项操作！", MessageBox.Icon.WARNING);
            return;
        }

        if (TfaDept_Master_Status.SelectedIndex == -1)
        {
            Err.Warning("请选择审核状态！", MessageBox.Icon.WARNING);
            return;
        }

        int id = int.Parse(rID.Value.ToString());
        //记录该条操作记录
        CxRemark cxr = new CxRemark();
        cxr.Guid = new Guid(Tfguid.Value.ToString());
        cxr.TypeID = 1;
        cxr.UID = int.Parse(Cookies.getCookies("cUid"));
        if ((Cookies.getCookies("cDeptID") == "3") && (Cookies.getCookies("cLevelID") == "1"))
        {

            if (TfaDept_master_remark.Text.Trim() == "")
            {
                Err.Warning("审核意见不能为空！", MessageBox.Icon.WARNING);
                return;
            }
            cxr.Remark = TfaDept_master_remark.Text;
            cxr.TStatus = int.Parse(TfaDept_Master_Status.SelectedItem.Value.ToString());

        }
        if ((Cookies.getCookies("cDeptID") == "1") && (Cookies.getCookies("cLevelID") == "1"))
        {
            if (TfBoss_Status.SelectedIndex == -1) { Err.Warning("请选择审核状态！", MessageBox.Icon.WARNING); return; }
            if (TfBoss_Remark.Text.Trim() == "") { Err.Warning("审核意见不能为空！", MessageBox.Icon.WARNING); return; }
            cxr.Remark = TfBoss_Remark.Text;
            cxr.TStatus = int.Parse(TfBoss_Status.SelectedItem.Value.ToString());
        }
        if (((Cookies.getCookies("cDeptID") != "3") && (Cookies.getCookies("cDeptID") != "1")) && (Cookies.getCookies("cLevelID") == "1"))
        {
            if (TfDept_master_status_Comb.SelectedIndex == -1) { Err.Warning("请选择审核状态！", MessageBox.Icon.WARNING); return; }
            if (TfDept_Master_Remark.Text.Trim() == "") { Err.Warning("审核意见不能为空！", MessageBox.Icon.WARNING); return; }
            cxr.Remark = TfDept_Master_Remark.Text;
            cxr.TStatus = int.Parse(TfDept_master_status_Comb.SelectedItem.Value.ToString());
        }
        cxr.InTime = DateTime.Now;
        cxr.Status = 1;
        cxr.Save();

        //更新主数据库
        CxStaffHr cx = new CxStaffHr("id", id);

        //人事领导数据
        if ((Cookies.getCookies("cDeptID") == "3") && Cookies.getCookies("cLevelID") == "1")
        {
            cx.ADeptMasterRemark = TfaDept_master_remark.Text;
            cx.ADeptMasterStatus = int.Parse(TfaDept_Master_Status.SelectedItem.Value);
            cx.ADeptMasterEnteringTime = DateTime.Now;
            if (TfaDept_Master_Status.SelectedItem.Value.ToString() != "2")
            {
                if (TfBoss_ID.SelectedIndex == -1)
                {
                    Err.Warning("请选择公司领导！", MessageBox.Icon.WARNING);
                    return;
                }
                if (TfDept_Master_ID.SelectedIndex == -1)
                {
                    Err.Warning("请选择部门领导！", MessageBox.Icon.WARNING);
                    return;
                }
                cx.DeptMasterId = int.Parse(TfDept_Master_ID.SelectedItem.Value.ToString());
                cx.BossUserID = int.Parse(TfBoss_ID.SelectedItem.Value.ToString());
                cx.DeptMasterStatus = 0;
                cx.BossStatus = 0;
            }
            
        }
        //公司领导审批框
        if ((Cookies.getCookies("cDeptID") == "1") && Cookies.getCookies("cLevelID") == "1")
        {
            cx.BossRemark = TfBoss_Remark.Text;
            cx.BossStatus = int.Parse(TfBoss_Status.SelectedItem.Value.ToString());
            cx.BossTime = DateTime.Now;
        }
        //部门领导审批框
        if ((Cookies.getCookies("cDeptID") == getDeptId(int.Parse(TfDeptID.Value.ToString())).ToString()) && Cookies.getCookies("cLevelID") == "1")
        {
            cx.DeptMasterRemark = TfDept_Master_Remark.Text;
            cx.DeptMasterStatus = int.Parse(TfDept_master_status_Comb.SelectedItem.Value.ToString());
            cx.DeptMasterEnteringTime = DateTime.Now;
        }
        cx.Save();

        Clear();

        Err.Warning("批复成功!", MessageBox.Icon.NONE);
    }
    protected void Clear()
    {
        //清空表单
        FormPanel1.Title = "员工诚信档案记录表";
        TfDeptName.Text = "";
        TfUserId.Text = "";
        TfDeptID.Value = "";
        TfTalkTime.Value = "";
        TfTalkContent.Text = "";
        TfPunish_party_reason.Text = "";
        TfPunish_party_time.Value = "";
        TfPunish_penal_reason.Text = "";
        TfPunish_penal_time.Value = "";
        TfParty_Problem.Text = "";
        TfOther_Problem.Text = "";
        rID.Value = "";
        IDLabel.Text = "";
        TfUserName.Text = "";
        TfUserId.Text = "";
        TfEntering_username.Text = "";
        TfaDept_master_remark.Text = "";
        TfBoss_ID.Reset();
        TfDept_Master_ID.Reset();
        TfaDept_Master_Status.Reset();
        tffileshow.Text = "";
        TfDept_Master_Remark.Text = "";
        TfBoss_Remark.Text = "";
        TfPunish_party_type.Text = "";
        TfPunish_penal_type.Text = ""; 
    }
    protected void Store1Bind()
    {
        Query q = new Query(VwStaffHrStep1.Schema);
        q.SetSelectList(
            "id as SID,ED_Username,ED_DeptName,aDept_Master_Remark,aDept_Master_Entering_time,aDept_Master_Status,Boss_Status,Dept_Master_Status");
        if((Cookies.getCookies("cDeptID")!="3")&&(Cookies.getCookies("cDeptID")!="1"))
        {
            q.AddWhere("Dept_master_ID", Cookies.getCookies("cUid"));
            q.AddWhere("adept_master_status", 1);
            q.AddWhere("dept_master_status",0);
        }
        if(Cookies.getCookies("cDeptID")=="1")
        {
            q.AddWhere("Boss_UserID", Cookies.getCookies("cUid"));
            q.AddWhere("adept_master_status", 1);
            q.AddWhere("dept_master_status", 1);
            q.AddWhere("boss_status", SubSonic.Comparison.NotEquals, 2);
        }
        if(Cookies.getCookies("cDeptID")=="3")
        {
            q.AddWhere("aDept_MasterID", Cookies.getCookies("cUID"));
            q.AddWhere("adept_master_status", SubSonic.Comparison.NotEquals, 2); 
        }
        q.ORDER_BY("id desc");
        Store1.DataSource = q.ExecuteReader();
        Store1.DataBind();
    }
    protected void Store3Bind()
    {
        int deptId = int.Parse(TfDeptID.Text);
        if(deptId>10)
        {
            Query q = new Query(CxSysDept.Schema);
            q.SetSelectList("deptid");
            q.AddWhere("ID", deptId);
            deptId = int.Parse(q.ExecuteScalar().ToString());
        }
        Query q2 = new Query(CxUserBase.Schema);
        q2.SetSelectList("username,uID");
        q2.AddWhere("levelid", 1);
        q2.AddWhere("depid", deptId);
        Store3.DataSource = q2.ExecuteDataSet();
        Store3.DataBind();
        
    }
    protected int getDeptId(int deptId)
    {
        if (deptId > 10)
        {
            Query q = new Query(CxSysDept.Schema);
            q.SetSelectList("deptid");
            q.AddWhere("ID", deptId);
            deptId = int.Parse(q.ExecuteScalar().ToString());
        }
        return deptId;
    }
    protected void Store1_OnRefreshData(object s, StoreRefreshDataEventArgs e)
    {
        Store1Bind();
    }
}
